<?php
/**
 * 《电话黑名单》实现类
 *
 * @author 孙晓晔
 * @version $Id: Black.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/TelUtil.class.php');

class Black extends TelUtil {
	public $tbl = Constant::tbl_blacklist;

	public $black = array(
		'-' => '请选择',
		'1' => '号码',
		'2' => '区号',
		'3' => '排序',
		'4' => '创建时间',
		'5' => '更新时间',
	);

	public $status = array(
		'-' => '请选择',

		'1' => '禁止',
		'2' => '允许',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'exten' => '分机',
		'number' => '号码',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);

		$this->dsn = 'emp';
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$province = $this->code_option('4');
		$controller->assign_by_ref('province_option', $province);

		$controller->display($request, 'tel/blacklist/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$province_s = $request['province_s'];
		$zone_s = $request['zone_s'];

		$where = " WHERE 1=1";

		if (!empty($province_s) && $province_s <> '-') {
			$where .= " AND code = '" . $province_s . "'";
		}

		if (!empty($zone_s)) {
			$where .= " AND zone = '" . $zone_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'tel/blacklist/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$uniqueid = $request['id'];

		$cdr = $request['cdr_s'];
		if (empty($cdr) || $cdr == '-') {
			$cdr = 'cdr';
		}

		$db = newdb($cdr);

		$fmt = "SELECT * FROM %s WHERE uniqueid = '%s'";
		$sql = sprintf($fmt, Constant::tbl_cdr, $uniqueid);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		
		// $row['dst'] = ltrim($row['dst'], "917909|17011|9", "");	
		$row['dst'] = $this->_a($row['dst']);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'tel/blacklist/add');
		return true;
	}
	
	function _a($s) {
		if ($this->_inner($s)) {
			return $s;
		}
		
		if (preg_match("/^9/", $s)) {
			return $this->_ct(substr($s, 1));
		}
		
		return $this->_ip($s);
	}
	
	function _inner($s) {
		if (preg_match("/^4\d{1,6}$/", $s)) {
			return true;
		}

		if (preg_match("/^\d{1,4}$/", $s)) {
			return true;
		}

		if (preg_match("/^(2|6|8)\d{3}$/", $s)) {
			return true;
		}

		return false;
	}
	
	function _ct($s) {
		if (preg_match("/^00/", $s)) {
			return substr($s, 2);
		}
		
		if (preg_match('/^(17909|11808|96688)00/i', $s)) {
			return substr($s, 7);
		}
		
		if (preg_match('/^(17909|11808|96688)0/i', $s)) {
			return $this->_ct_gn(substr($s, 6));
		}
		
		return $s;
	}
	
	function _ct_gn($s) {
		if (preg_match('/^1(3|4|5|7|8)\d{9}$/i', $s)) {
			return $s;
		}
		
		return '0' . $s;
	}
	
	function _ip($s) {
		if (preg_match("/^(171)/", $s)) {
			return substr($s, 2);
		}
		
		if (preg_match("/^(17011|19100)/", $s)) {
			return $this->_ip_gn(substr($s, 5));
		}
		
		return $s;
	}
	
	function _ip_gn($s) {
		if (preg_match("/^86/", $s)) {
			return $this->_ct_gn(substr($s, 2));
		}
		
		return $s;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$exten = $this->_input($request['exten']);
		$from = $this->_input($request['from']);

		$number = $request['number'];
		$zone = $this->_input($request['zone']);

		$content = addslashes($request['content']);

		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(exten, number, zone, `from`, content, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $exten, $number, $zone, $from, $content, $status);
		debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'tel/blacklist/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$exten = $this->_input($request['exten']);
		$from = $this->_input($request['from']);
		
		$number = $request['number'];
		$zone = $this->_input($request['zone']);

		$content = addslashes($request['content']);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET exten = '%s', number = '%s', zone = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $exten, $number, $zone, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'tel/blacklist/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'tel/blacklist/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['exten_o'] = $this->_number($row['exten']);
		$row['number_o'] = $this->_number($row['number']);
		$row['from_o'] = $this->_number($row['from']);
		
		$row['zone_t'] = $this->_zone_t($row['zone']);
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _zone_t($s) {
		if (empty($s)) {
			return '';
		}
		
		require_once(dirname(__FILE__) . '/../info/Zone.class.php');
		$zone = new Zone();
		
		if ($a = $zone->_byzone($s)) {
			return sprintf("%s%s [%s]", $a['province'], $a['city'], $s);
		}
		
		return $s;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function importPost($controller, $request) {
		$this->pretreat($controller);

		$data = $request['data'];
		// echo $data;

		$aa = preg_split("/[\\n]+/i", $data);
		// print_r($a);

		$province = '';
		$db = newdb($this->dsn);
		while (list($k, $v) = each($aa)) {
			$buf = trim($v);
			if (strlen($buf) < 5) {
				continue;
			}

			$a = preg_split("/[\t|\s]+/i", $buf);
			// print_r($a);
			if (count($a) < 2) {
				$province = $buf;
				continue;
			}

			$city = trim($a[0]);
			$zone = trim($a[1]);

			$fmt = "INSERT INTO %s(`province`, city, zone) VALUES('%s', '%s', '%s')";
			$sql = sprintf($fmt, $this->tbl, $province, $city, $zone);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_add($controller, $request) {
		$this->pretreat($controller);

		$number = $request['number'];
		$name = $request['name'];

		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s WHERE code = '%'";
		$sql = sprintf($fmt, $this->tbl, $code);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return false;
		}

		$fmt = "INSERT INTO %s(number, name, createtime) VALUES('%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $number, $name);
		// debug($sql);
		$db->query($sql);

		return true;
	}

	function _permit($exten, $code) {
		return $this->_strategy($exten, $code, '1');
	}

	function _deny($exten, $code) {
		return $this->_strategy($exten, $code, '2');
	}

	function _strategy($exten, $code, $status = '2') {
		if (empty($code)) {
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '%s' and code like '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $code);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if (empty($row['exten']) || $row['exten'] == $exten) {
				return true;
			}
		}

		$zone = $this->_zone($code);
		if (empty($zone)) {
			return false;
		}

		$fmt = "SELECT * FROM %s WHERE status = '%s' and zone = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $zone);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if (empty($row['exten']) || $row['exten'] == $exten) {
				return true;
			}
		}

		return false;
	}

	function _zone($code) {
		if (preg_match('/^1(3|4|5|7|8)\d{9}$/i', $s)) {
			$db = newdb($this->dsn_r);
			$fmt = "SELECT * FROM %s WHERE mobile = '%s'";
			$sql = sprintf($fmt, Constant::tbl_mobile, substr($code, 0, 7));
			// debug($sql);
			$result = $db->query($sql);
			if ($row = $db->fetch_array($result)) {
				return $row['zone'];
			} else {
				return '';
			}
		}

		if (preg_match('/^0(10|2)/i', $s)) {
			return substr($code, 0, 3);
		}

		return substr($code, 0, 4);
	}
	
	function _emp_by_exten($s) {
		
	}
}
?>